home *** CD-ROM | disk | FTP | other *** search
/ Freelog 22 / freelog 22.iso / Prog / Djgpp / GPC2952B.ZIP / doc / gpc / demos / schemademo.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-02-09  |  2.5 KB  |  84 lines

  1. {
  2. GPC demo program about schemata (Extended Pascal feature).
  3. Schemata are the Pascal way to get dynamic arrays without dirty
  4. tricks (like `GetMem' with computed sizes). Although GPC supports
  5. these dirty tricks, too, their use is not recommended.
  6.  
  7. Copyright (C) 1999-2001 Free Software Foundation, Inc.
  8.  
  9. Author: Frank Heckenbach <frank@pascal.gnu.de>
  10.  
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License as
  13. published by the Free Software Foundation, version 2.
  14.  
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19.  
  20. You should have received a copy of the GNU General Public License
  21. along with this program; see the file COPYING. If not, write to
  22. the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. Boston, MA 02111-1307, USA.
  24.  
  25. As a special exception, if you incorporate even large parts of the
  26. code of this demo program into another program with substantially
  27. different functionality, this does not cause the other program to
  28. be covered by the GNU General Public License. This exception does
  29. not however invalidate any other reasons why it might be covered
  30. by the GNU General Public License.
  31. }
  32.  
  33. program SchemaDemo;
  34.  
  35. type
  36.   Natural = 1 .. MaxInt;
  37.  
  38.   { A schema type. `Count' is the discriminant which can influence the size
  39.     of the type. Discriminants must be specified when declaring variables,
  40.     function return types and when allocating a schema variable with `New',
  41.     but parameters and pointer types can refer to undiscriminated schemata. }
  42.   TIntegers (Size : Integer) = array [1 .. Size] of Integer;
  43.  
  44.   { A pointer to this schema type. }
  45.   PIntegers = ^TIntegers;
  46.  
  47. { A procedure taking a schema parameter }
  48. procedure Test (const Foo : TIntegers);
  49. var i : Integer;
  50. begin
  51.   Writeln ('The procedure was called with a schema of size ', Foo.Size, ' with the value:');
  52.   Write ('(');
  53.   for i := 1 to Foo.Size do
  54.     begin
  55.       Write (Foo [i]);
  56.       if i < Foo.Size then Write (', ')
  57.     end;
  58.   Writeln (')');
  59.   Writeln
  60. end;
  61.  
  62. const
  63.   Foo : TIntegers (3) = (2, 3, 17);
  64.  
  65. var
  66.   a, i : Integer;
  67.  
  68. begin
  69.   Test (Foo);
  70.  
  71.   repeat
  72.     Write ('Enter the size for a schema array: ');
  73.     Readln (a)
  74.   until a >= 1;
  75.  
  76.   { A schema array declared in the statement part (GPC extension) }
  77.   var t : TIntegers (a);
  78.  
  79.   Writeln ('Enter the contents of the schema array:');
  80.   for i := 1 to a do Read (t [i]);
  81.   Writeln;
  82.   Test (t)
  83. end.
  84.